package com.lovepick.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lovepick.R;
import com.lovepick.bean.Order;
import com.lovepick.util.OrderState;

public class OrderAdapter extends BaseAdapter
{
	private static final int ORDER_NORMAL = 0;
	private static final int ORDER_FINISHED = 1;
	private static final int ORDER_APPLY = 2;

	private List<Order> mOrders = null;
	private Context mContext;
	private LayoutInflater mInflater;


	public OrderAdapter(Context context, List<Order> orders)
	{
		this.mContext = context;
		this.mOrders = orders;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return mOrders.size();
	}

	@Override
	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return mOrders.get(position);
	}

	public void addItem(Order order)
	{
		mOrders.add(order);
		notifyDataSetChanged();
	}

	public void addAll(List<Order> orders)
	{
		mOrders.addAll(orders);
		notifyDataSetChanged();
	}

	public void removeItem(int location)
	{
		mOrders.remove(location);
		notifyDataSetChanged();
	}

	public void removeItem(Order order)
	{
		mOrders.remove(order);
		notifyDataSetChanged();
	}

	public void removeAll(List<Order> orders)
	{
		mOrders.removeAll(orders);
		notifyDataSetChanged();
	}

	@Override
	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getViewTypeCount()
	{
		return 3;
	}

	@Override
	public int getItemViewType(int position)
	{
		Order order = mOrders.get(position);
		if (OrderState.FINISHED.equals(order.getState()))
		{
			return ORDER_FINISHED;
		}
		else if (OrderState.TO_DEAL.equals(order.getState()))
		{
			return ORDER_NORMAL;
		}
		else
		{
			return ORDER_APPLY;
		}
	}

	public void uncheckAll()
	{
		for (Order order : mOrders)
		{
			order.setChecked(false);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		Order order = mOrders.get(position);
		if (null == convertView)
		{
			convertView = mInflater.inflate(R.layout.order_item_others, null);

			viewHolder = new ViewHolder();
			viewHolder.src = (TextView) convertView.findViewById(R.id.order_item_others_src);
			viewHolder.dest = (TextView) convertView.findViewById(R.id.order_item_others_dest);
			viewHolder.nickName = (TextView) convertView.findViewById(R.id.order_item_others_user);
			viewHolder.goTime = (TextView) convertView.findViewById(R.id.order_item_others_gotime);
			viewHolder.count = (TextView) convertView.findViewById(R.id.order_item_others_count);
			viewHolder.money = (TextView) convertView.findViewById(R.id.order_item_others_money);
			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.nickName.setText(order.getNickName());
		viewHolder.src.setText(order.getSrc());
		viewHolder.dest.setText(order.getDest());
		viewHolder.goTime.setText(order.getGoTime());
		viewHolder.count.setText(String.valueOf(order.getCount()));
		viewHolder.money.setText(String.valueOf(order.getMoney()));

		return convertView;
	}

	static class ViewHolder
	{
		public TextView nickName, src, dest, goTime, count, money;
	}

}
